global class EventSyncClass {
    /*
    ============================================================
    11-1日创建： lisiheng
     Trigger : EventSync 辅助类，详情查看EventSync；
    ============================================================
    11-8日最新修改：lisiheng
    详情看trigger；
    ============================================================
    */
    @future
    public static void SortMethod(id ert/*event上的callplan recordtype*/,string owner/*当前用户ID*/,id psrt/*event上的hcp活动ID*/,id pcrt/*event上的妈妈班ID*/,list<id> IDlist) {
    
     list<event> einsert=new list<event>();
     list<callplan__c> cplist=[select id,callplandate__c,callworkingtype__c,hospital__c,ownerid__c from callplan__c where id in:idlist];
     list<datetime> cpdate=new list<datetime>();
     Datetime newdate;
     
     for(callplan__c t:cplist){
         if(t.CallWorkingtype__c!='妈妈班' && t.CallWorkingtype__c!='科内会'){
                        Event e = new Event(Subject=t.CallWorkingtype__c, 
                                        WhatId=t.id,
                                        recordtypeid=ert,
                                        ownerid=t.ownerid__c,
                                        StartDateTime=datetime.newInstancegmt(t.CallPlanDate__c.year(),t.CallPlanDate__c.month(),t.CallPlanDate__c.day(), 0, 0, 0),
                                        EndDateTime=datetime.newInstancegmt(t.CallPlanDate__c.year(),t.CallPlanDate__c.month(),t.CallPlanDate__c.day(), 0, 30, 0),
                                        IsReminderSet = false,
                                        hospitalname__c=t.hospital__c,
                                        eventdate__c=t.callplandate__c
                                         );
                         einsert.add(e);
         }
 
       
        
         newdate = datetime.newInstancegmt(t.CallPlanDate__c.year(),t.CallPlanDate__c.month(),t.CallPlanDate__c.day(), 0, 0, 0);
         for(integer i=1;i<=28;i++){
            cpdate.add(newdate);
            newdate=newdate.addMinutes(30);
          }
     }
     if(einsert.size()>0){
          insert einsert;
      }
    list<event> all=[select id,StartDateTime,whatid,recordtypeid from event e where e.ownerid=:owner and e.startdatetime in :cpdate order by hospitalname__c];
    map<date,datetime> dateMap=new map<date,datetime>();
    
    list<event> pocketl=new list<event>();
    list<event> pclassl=new list<event>();
    list<event> callplanl=new list<event>();
    for(event e:all){
        datemap.put(e.startdatetime.date(),datetime.newInstancegmt(e.startdatetime.date().year(),e.startdatetime.date().month(),e.startdatetime.date().day(), 0, 0, 0));
        if(e.recordtypeid==psrt){
            pocketl.add(e);
        }
        if(e.recordtypeid==pcrt){
            pclassl.add(e);
        }
        if(e.recordtypeid==ert){
            callplanl.add(e);
        }
    }
    list<event> insertl=new list<event>();
    for(event p:pocketl){
        datetime dtime=datemap.get(p.startdatetime.date());
        p.StartDateTime=dtime;
        dtime=dtime.addHours(1);
        datemap.put(p.startdatetime.date(),dtime);
        p.EndDateTime=dtime;
        insertl.add(p);
    }
    for(event p:pclassl){
        datetime dtime=datemap.get(p.startdatetime.date());
        p.StartDateTime=dtime;
        dtime=dtime.addHours(1);
        datemap.put(p.startdatetime.date(),dtime);
        p.EndDateTime=dtime;
        insertl.add(p);
    }
    for(event p:callplanl){
        datetime dtime=datemap.get(p.startdatetime.date());
        p.StartDateTime=dtime;
        dtime=dtime.addMinutes(30);
        datemap.put(p.startdatetime.date(),dtime);
        p.EndDateTime=dtime;
        insertl.add(p);
    }
    
    list<event> updateevent=new list<event>();
    for(integer i=0;i<insertl.size();){
        updateevent.add(insertl[i]);
        while(updateevent.size()==100 || i==insertl.size()-1){
            update updateevent;
            updateevent.clear();
            break;
        }
        i++;
    }

    }//end bracket of SortMethod(insert part)
    
    @future
    public static void SortMethod(id ert/*event上的callplan recordtype*/,string owner/*当前用户ID*/,id psrt/*event上的hcp活动ID*/,id pcrt/*event上的妈妈班ID*/,list<id> IDlist,set<date> olddatel) {
        list<datetime> dtimel=new list<datetime>();
        datetime newdate;
        list<callplan__c> cplist=[select id,callplandate__c from callplan__c where id in:idlist];
        map<id,date> cpMap=new map<id,date>();//store id and callplandate information,and mapping them
        for(callplan__c c:cplist){
            cpmap.put(c.id,c.callplandate__c);
            newdate = datetime.newInstancegmt(c.CallPlanDate__c.year(),c.CallPlanDate__c.month(),c.CallPlanDate__c.day(), 0, 0, 0);
            for(integer i=1;i<=28;i++){
               dtimel.add(newdate);
               newdate=newdate.addMinutes(30);
             }
        }
       for(date d:olddatel){
           newdate = datetime.newInstancegmt(d.year(),d.month(),d.day(), 0, 0, 0);
            for(integer i=1;i<=28;i++){
               dtimel.add(newdate);
               newdate=newdate.addMinutes(30);
             }
       }
        list<event> MoveList=[select id,whatid,startdatetime from event where whatid in :cpmap.keyset()];
        for(event e:MoveList){
            e.startdatetime=datetime.newInstancegmt(cpmap.get(e.whatid).year(),cpmap.get(e.whatid).month(),cpmap.get(e.whatid).day(), 0, 0, 0);
            e.eventdate__c=cpmap.get(e.whatid);
        }
        update movelist;//move event to call date;
        
        list<event> ESortList=[select id,StartDateTime,whatid,recordtypeid from event where ownerid=:owner and startdatetime in:dtimel order by hospitalname__c];
        map<date,datetime> dateMap=new map<date,datetime>();
        
        list<event> pocketl=new list<event>();
        list<event> pclassl=new list<event>();
        list<event> callplanl=new list<event>();
        for(event e:ESortList){
            datemap.put(e.startdatetime.date(),datetime.newInstancegmt(e.startdatetime.date().year(),e.startdatetime.date().month(),e.startdatetime.date().day(), 0, 0, 0));
            if(e.recordtypeid==psrt){
                pocketl.add(e);
            }
            if(e.recordtypeid==pcrt){
                pclassl.add(e);
            }
            if(e.recordtypeid==ert){
                callplanl.add(e);
            }
        }
        list<event> updatel=new list<event>();
        for(event p:pocketl){
            datetime dtime=datemap.get(p.startdatetime.date());
            p.StartDateTime=dtime;
            dtime=dtime.addHours(1);
            datemap.put(p.startdatetime.date(),dtime);
            p.EndDateTime=dtime;
            updatel.add(p);
        }
        for(event p:pclassl){
            datetime dtime=datemap.get(p.startdatetime.date());
            p.StartDateTime=dtime;
            dtime=dtime.addHours(1);
            datemap.put(p.startdatetime.date(),dtime);
            p.EndDateTime=dtime;
            updatel.add(p);
        }
        for(event p:callplanl){
            datetime dtime=datemap.get(p.startdatetime.date());
            p.StartDateTime=dtime;
            dtime=dtime.addMinutes(30);
            datemap.put(p.startdatetime.date(),dtime);
            p.EndDateTime=dtime;
            updatel.add(p);
        }
        
        list<event> updateevent=new list<event>();
        for(integer i=0;i<updatel.size();){
            updateevent.add(updatel[i]);
            while(updateevent.size()==100 || i==updatel.size()-1){
                update updateevent;
                updateevent.clear();
                break;
            }
            i++;
        }
        
    }//end bracket of SortMethod(update part);
    
}//end bracket of class